import {getGoodsDetail} from "../../api/goods.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    isshow:false,
    goods:{},
    skuList:[],
    cart_goods_img:"",
    sku_name:"",
    sku_price:0,
    sku_stock:0,
    sku_id:0,
    selected:false,
    num:0,
    sku:{},
    total:0
  },

  /**
   * 页面加载获取商品详细信息
   */
  async initData(){
    let res = await getGoodsDetail({id:this.data.id})
    if(res.code==200){
      this.setData({
        goods:res.data,
        skuList:res.data.skuList,
        cart_goods_img:res.data.main_img_url,
        sku_name:res.data.name,
        sku_price:res.data.price_fee,
        sku_stock:res.data.stock
      })
    }
  },

  /**
   * 选择商品规格
   */
  selectAttr(event){
    let sku = event.currentTarget.dataset.sku;
    this.setData({
      selected:true,
      sku_name:sku.sku_name,
      sku_price:sku.price_fee,
      sku_stock:sku.actual_stock,
      sku_id:sku.sku_id,
      num:1,
      sku:sku
    })
  },

  /**
   * 点击加入购物车出发的函数
   */
  joinCart(){
    this.setData({
      isshow:true
    })
  },
  /**
   * 添加到购物车列表中
   */
  submit(){
    if(this.data.num==0){
      return;
    }
     //1. 获取本地购物列表  
     let carlist=wx.getStorageSync('carlist');
     if(carlist){
       //不为空
        //遍历 查询列表中是否有新加入的商品
        let flag=false;//列表中默认没有要新加入的商品

        for(let goods of carlist){
          if(goods.sku_id==this.data.sku_id){
            //找到了  修改数量 
            let tmp=goods.num+this.data.num;
            goods.num=tmp;
            flag=true;
            //终止循环
            break;
          }
        }
        if(!flag){
          //购物列表没有 直接加入 
          let info={};
          info = this.data.sku;
          info.spu_name = this.data.goods.name;
          info.img_url = this.data.cart_goods_img;
          info.shop_id=this.data.goods.shop_id;
          info.brand_id=this.data.goods.brand_id;
          info.category_id=this.data.goods.category_id;
          info.shop_category_id = this.data.goods.shop_category_id;
          info.checkd=true;
          info.num=this.data.num;
            //放入购物列表 
          carlist.push(info);
        } 
     } else {
        carlist=[];
        let info={};
        info = this.data.sku;
        info.spu_name = this.data.goods.name;
        info.img_url = this.data.cart_goods_img;
        info.shop_id=this.data.goods.shop_id;
        info.brand_id=this.data.goods.brand_id;
        info.category_id=this.data.goods.category_id;
        info.shop_category_id = this.data.goods.shop_category_id;
        info.checkd=true;
        info.num=this.data.num;
        // this.setData({
        //   total:info.num
        // });
        //放入购物列表 
        carlist.push(info);
      }
      let tmpTotal=0;
       carlist.forEach(el=>{
        tmpTotal+=parseInt(el.num);
       });

       this.setData({
         "total":tmpTotal
       });

          //存储到缓存 
          wx.setStorageSync("carlist",carlist);
          wx.setStorageSync("goods_total",tmpTotal);
      this.close()

  },

  /**
   * 关闭遮罩
   */
 close(){
    this.setData({
      isshow:false
    })
  },

  /**
   * 增加数量
   */
  upNum(){
    if(!this.data.selected)
    return;
    let tmp = this.data.num+1
    this.setData({
      num:tmp
    })
  },
  /**
   * 减少数量
   */
  delNum(){
    if(this.data.num<=1)
      return;
    let tmp = this.data.num-1
    this.setData({
      num:tmp
    })
  },

  /**
   * 跳转首页
   */
  toHome(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  toCart(){
    wx.switchTab({
      url: '/pages/cart/index',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let num = wx.getStorageSync('goods_total')
    if (num){
      this.setData({
        total:num
      })
    } else{
      this.setData({
        total:0
      })
    }
    this.setData({
      id : options.id
    })
    
    this.initData();
      
    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})